package com.xhh.nphm.api.controller;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.springframework.stereotype.Component;

@Component
public class ConDatabaseBySqlServer
{
  private String driver;
  private String url;
  private String username;
  private String password;
  private Connection con = null;
  private Statement st = null;
  ResultSet rs = null;

  public void initialize()
  {
    Properties p = new Properties();
    try {
      p.load(ConDatabaseBySqlServer.class.getClassLoader()
        .getResourceAsStream("jdbc.properties"));
      this.username = p.getProperty("jdbc.username");
      this.password = p.getProperty("jdbc.password");
      this.url = p.getProperty("jdbc.url");
      this.driver = p.getProperty("jdbc.driverClassName");
    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException(
        "读取数据库配置文件失败！");
    }
  }

  public ConDatabaseBySqlServer()
  {
    initialize();
    try {
      Class.forName(this.driver);
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  }

  public Connection getConnection()
  {
    Connection conn = null;
    try {
      conn = DriverManager.getConnection(this.url, this.username, this.password);
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return conn;
  }

  private void getCon()
  {
    if (this.con == null)
      try
      {
        this.con = DriverManager.getConnection(this.url, this.username, this.password);
      } catch (SQLException e) {
        e.printStackTrace();
      }
  }

  public ResultSet executeSql(String sql)
  {
    getCon();
    try {
      this.st = this.con.createStatement();
      this.rs = this.st.executeQuery(sql);
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return this.rs;
  }

  public Boolean executeUpdate(String sql)
  {
    getCon();
    try {
      this.st = this.con.createStatement();
      this.st.executeUpdate(sql);
    } catch (SQLException e) {
      e.printStackTrace();
      return Boolean.valueOf(false);
    }
    return Boolean.valueOf(true);
  }

  public void closeConnection()
  {
    try
    {
      if (this.rs != null)
      {
        this.rs.close();
        this.rs = null;
      }
      if (this.st != null)
      {
        this.st.close();
        this.st = null;
      }
      if (this.con != null)
      {
        this.con.close();
        this.con = null;
      }
    }
    catch (SQLException e) {
      e.printStackTrace();
    }
  }
}

/* Location:           F:\demo\ROOT\ROOT\WEB-INF\classes\
 * Qualified Name:     com.xhh.api.controller.ConDatabaseBySqlServer
 * JD-Core Version:    0.6.0
 */